/*
 * DefaultIntervalAnnotationInfoPanel.java
 *
 * Created on 18 de julio de 2007, 14:41
 */

package net.javahispano.jsignalwb.plugins.defaults;

import java.awt.Color;
import java.awt.Window;
import java.beans.*;
import java.util.Date;

import javax.swing.*;

import net.javahispano.jsignalwb.JSWBManager;
import net.javahispano.jsignalwb.jsignalmonitor.TimeRepresentation;
import org.joda.time.DateTime;

/**
 *
 * @author  Compaq_Propietario
 */
public class DefaultIntervalAnnotationInfoPanel extends javax.swing.JPanel implements PropertyChangeListener {

    private DefaultIntervalAnnotation dim;
    private JColorChooser jcc;
    private JWindow jw;
    /** Creates new form DefaultIntervalAnnotationInfoPanel */
    public DefaultIntervalAnnotationInfoPanel(DefaultIntervalAnnotation dia) {
        this.dim = dia;
        initComponents();
        markTitleTextField.setText(dim.getTitle());
        //markTimeTextField.setText(TimeRepresentation.timeToString(dim.getMarkTime()));
        //endTimeTextField.setText(TimeRepresentation.timeToString(dim.getEndTime()));
        jTextFieldDate1.setText(TimeRepresentation.timeToString(
                dim.getAnnotationTime()));
        jTextFieldDate2.setText(TimeRepresentation.timeToString(
                dim.getEndTime()));
        datePicker1.showButtonOnly(true);
        datePicker2.showButtonOnly(true);
        try {
            datePicker1.setDate(new Date(dim.getAnnotationTime()));
            datePicker2.setDate(new Date(dim.getEndTime()));
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
        datePicker1.addPropertyChangeListener(datePicker1.PROPERTY_NAME_DATE, this);
        datePicker2.addPropertyChangeListener(datePicker2.PROPERTY_NAME_DATE, this);
        comentaryTextArea.setText(dim.getComentary());
        kindLabel.setText("Kind of Annotation: " + dim.getName());
        colorTextField.setBackground(dim.getColor());
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ("date".equals(evt.getPropertyName())) {
            if (evt.getSource().equals(datePicker1)) {
                Date date = datePicker1.getDate();
                if (date != null) {
                    jTextFieldDate1.setText(TimeRepresentation.timeToString(
                            swapDateNoChangeTime(
                                    dim.getAnnotationTime(), date.getTime())));
                }
            } else if (evt.getSource().equals(datePicker2)) {
                Date date = datePicker2.getDate();
                if (date != null) {
                    jTextFieldDate2.setText(TimeRepresentation.timeToString(
                            swapDateNoChangeTime(
                                    dim.getEndTime(), date.getTime())));
                }
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        markTitleTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDate1 = new net.javahispano.jsignalwb.ui.JTextFieldDate();
        jTextFieldDate2 = new net.javahispano.jsignalwb.ui.JTextFieldDate();
        datePicker1 = new com.michaelbaranov.microba.calendar.DatePicker();
        datePicker2 = new com.michaelbaranov.microba.calendar.DatePicker();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        comentaryTextArea = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        kindLabel = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        colorTextField = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 153, 0), 5, true));
        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setForeground(java.awt.Color.blue);
        jLabel2.setText("Start time:");

        jLabel3.setForeground(java.awt.Color.blue);
        jLabel3.setText("Mark Title:");

        markTitleTextField.setForeground(java.awt.Color.blue);
        markTitleTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        markTitleTextField.setText("jTextField1");

        jLabel1.setForeground(java.awt.Color.blue);
        jLabel1.setText("End time:");

        jTextFieldDate1.setForeground(java.awt.Color.blue);
        jTextFieldDate1.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jTextFieldDate2.setForeground(java.awt.Color.blue);
        jTextFieldDate2.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel5.setForeground(java.awt.Color.blue);
        jLabel5.setText("Category:");

        jTextField1.setForeground(java.awt.Color.blue);
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText(dim.getCategory());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel5))
                          .addGap(6, 6, 6)
                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                              .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.
                Alignment.LEADING)
                .addComponent(jTextFieldDate2, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addComponent(jTextFieldDate1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.
                Alignment.LEADING)
                .addComponent(datePicker1, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(datePicker2, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 231,
                                                  Short.MAX_VALUE)
                                    .addComponent(markTitleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 231,
                                                  Short.MAX_VALUE))
                          .addContainerGap())
                );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(markTitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                              .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.
                Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(jTextFieldDate1, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8,
                Short.MAX_VALUE)
                                              .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.
                Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jTextFieldDate2, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addComponent(datePicker1, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addComponent(datePicker2, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                          .addContainerGap())
                );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel4.setForeground(java.awt.Color.blue);
        jLabel4.setText("Comment");

        comentaryTextArea.setColumns(20);
        comentaryTextArea.setRows(5);
        jScrollPane1.setViewportView(comentaryTextArea);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288,
                                                  Short.MAX_VALUE)
                                    .addComponent(jLabel4))
                          .addContainerGap())
                );
        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jLabel4)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                          .addContainerGap())
                );

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1hideJWindow(evt);
            }
        });

        kindLabel.setForeground(java.awt.Color.blue);
        kindLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        kindLabel.setText("Kind of Mark:");
        kindLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        colorTextField.setBackground(dim.getColor());
        colorTextField.setEditable(false);
        colorTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        colorTextField.setText("Mark Color");

        jButton3.setText("Choose color");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(colorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jButton3)
                          .addContainerGap())
                );
        jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(colorTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton3))
                          .addContainerGap())
                );

        jButton4.setText("Cancel");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                              .addComponent(jButton2)
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114,
                Short.MAX_VALUE)
                                              .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
                javax.swing.GroupLayout.PREFERRED_SIZE)
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addComponent(jButton4))
                                    .addComponent(kindLabel, javax.swing.GroupLayout.Alignment.LEADING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(kindLabel)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButton4)
                                    .addComponent(jButton1)
                                    .addComponent(jButton2))
                          .addContainerGap())
                );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                              Short.MAX_VALUE)
                );
    } // </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton4ActionPerformed
        hideJWindow();
    } //GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton3ActionPerformed
        Color c = jcc.showDialog(this, "SelectColor", dim.getColor());
        if (c != null) {
            colorTextField.setBackground(c);
        }
    } //GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton2ActionPerformed
        if (JOptionPane.showConfirmDialog(jw.getParent(), "Are you sure?",
                                          "Delete Annotation",
                                          JOptionPane.YES_NO_OPTION,
                                          JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            JSWBManager.getJSWBManagerInstance().removeAnnotation(dim);
            hideJWindow();
        }

    } //GEN-LAST:event_jButton2ActionPerformed

    private void jButton1hideJWindow(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton1hideJWindow
        dim.setTitle(markTitleTextField.getText());
        dim.setComentary(comentaryTextArea.getText());
        long start = TimeRepresentation.stringToMillis(jTextFieldDate1.getText());
        long end = TimeRepresentation.stringToMillis(jTextFieldDate2.getText());
        dim.setAnnotationTime(Math.min(start, end));
        dim.setEndTime(Math.max(start, end));
        dim.setCategory(jTextField1.getText());
        dim.setColor(colorTextField.getBackground());
        hideJWindow();
    } //GEN-LAST:event_jButton1hideJWindow

    public void showJWindow(Window owner) {
        jw = new JWindow(owner);
        //jw.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());
        jw.add(this);
        jw.setSize(this.getPreferredSize());
        jw.setLocationRelativeTo(owner);
        jw.setVisible(true);
    }

    private void hideJWindow() {
        JSWBManager.getJSWBManagerInstance().refreshJSM(false);
        jw.dispose();
    }

    /**
     * @param old long
     * @param newTime long
     * @return long
     */
    private static long swapDateNoChangeTime(long old, long newTime) {
        DateTime dt = new DateTime(old);
        DateTime newDateTime = new DateTime(newTime).withTime(dt.getHourOfDay(),
                dt.getMinuteOfHour(), dt.getSecondOfMinute(),
                dt.getMillisOfSecond());

        return newDateTime.getMillis();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField colorTextField;
    private javax.swing.JTextArea comentaryTextArea;
    private com.michaelbaranov.microba.calendar.DatePicker datePicker1;
    private com.michaelbaranov.microba.calendar.DatePicker datePicker2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private net.javahispano.jsignalwb.ui.JTextFieldDate jTextFieldDate1;
    private net.javahispano.jsignalwb.ui.JTextFieldDate jTextFieldDate2;
    private javax.swing.JLabel kindLabel;
    private javax.swing.JTextField markTitleTextField;
    // End of variables declaration//GEN-END:variables

}
